
package com.viweb.storemanager.temp;

import java.awt.Color;
import java.awt.Component;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableCellRenderer;
import sun.swing.DefaultLookup;


public class NumberCellRenderer extends DefaultTableCellRenderer {

    private Color oddLine = new Color(240, 240, 255);
    private Color evenLine = Color.WHITE;
    private Color selectedLine = new Color(172, 210, 255);
    private Locale locale = Locale.US;

    /** Creates a new instance of NumberCellRenderer */
    public NumberCellRenderer() {
        super();
    }

    /** Creates a new instance of NumberCellRenderer */
    public NumberCellRenderer(Locale locale) {
        super();
        this.locale = locale;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {

        if (isSelected) {
            if (hasFocus) {
                this.setBackground(this.selectedLine);
            } else {
                this.setBackground(this.selectedLine);
            }
        } else {
            if (row % 2 == 0) {
                this.setBackground(this.evenLine);
            } else {
                this.setBackground(this.oddLine);
            }
        }

        if (hasFocus) {
            Border border = null;
            if (isSelected) {
                border = DefaultLookup.getBorder(this, ui, "Table.focusSelectedCellHighlightBorder");
            }
            if (border == null) {
                border = DefaultLookup.getBorder(this, ui, "Table.focusCellHighlightBorder");
            }
            this.setBorder(border);

            if (!isSelected && table.isCellEditable(row, column)) {
                Color col;
                col = DefaultLookup.getColor(this, ui, "Table.focusCellForeground");
                if (col != null) {
                    super.setForeground(col);
                }
                col = DefaultLookup.getColor(this, ui, "Table.focusCellBackground");
                if (col != null) {
                    super.setBackground(col);
                }
            }
        } else {
            this.setBorder(noFocusBorder);
        }

        this.setEnabled(table.isEnabled());
        this.setFont(table.getFont());
        this.setForeground(Color.BLACK);

        NumberFormat nf = NumberFormat.getNumberInstance(locale);
        DecimalFormat df = (DecimalFormat) nf;
        df.applyPattern("###,###");
        try {
            this.setText(" " + df.format(value) + " ");
        } catch (Exception ex) {
            this.setText(" " + value.toString() + " ");
        }

        return this;
    }
}
